package a11_单调栈;

import java.util.Arrays;
import java.util.Stack;

/**
 * <p>
 * a03_下一个更大元素2
 * </p>
 *
 * @author flyduck
 * @since 2025/3/7
 */
public class a03_下一个更大元素2 {
    public int[] nextGreaterElements(int[] nums) {
        int[] result = new int[nums.length * 2];
        Arrays.fill(result, -1);

        Stack<Integer> stack = new Stack<>();
        stack.push(0);

        for (int i = 1; i < nums.length * 2; i++) {
            while (!stack.isEmpty() && nums[stack.peek() % nums.length] < nums[i % nums.length]){
                Integer pop = stack.pop();
                result[pop] = nums[i % nums.length];
            }
            stack.push(i);
        }

        return Arrays.copyOfRange(result, 0, nums.length);
    }
}
